<!-- Discuss_TeacherList.html -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no, email=no, date=no, address=no">
    <title>教师端讨论列表</title>
    <link rel="stylesheet" href="../../css/weui.min.css"/>
    <link rel="stylesheet" href="../../css/mui.min.css"/>
    <link rel="stylesheet" href="../../css/base.css">
    <style type="text/css">
    	.mui-table-view-cell>a:not(.mui-btn){
    		padding: 10px;
    	}
    	.cell-title{
    		height: 22px;
    	}
    	.cell-subtitle,.message_content{
    		height: 20px;
    	}
    	.right-div{
    		width: 70px;
    		float: right;
    	}
    	.left-div{
    		margin-right: 70px;
    	}
    	.message_title{
    		width: 90%;
    	}
    	.right-tag{
    		text-align: right;
    		height: 20px;
    	}
    	.mui-table-view .mui-media-object{
    		/*margin-top: 10px;*/
    	}
    	.discuss-del{
    		width: 18px;
    		margin-right: 0px;
    	}
    	.discuss-del-div{
    		text-align: right;
    		/*height: 30px;*/
    	}
    	.discuss-none-del{
    		width: 18px;
    		margin-right: 0px;
    	}
    	.discuss-none-del-div{
    		text-align: right;
    		height: 40px;
    		visibility:hidden;
    	}
    	.mui-table-view-cell{
    		position: relative;
    	}
    	.tap-down-btn{
    		position: absolute;
    		right: 0;
    		top: 0;
    		width: 46px;
    		height: 62px;
    		padding: 12px 13px 28px 13px;
    	}
    	.tap-down-btn-img{
    		width: 22px;
    		height: 22px;
    	}
    	.hide-v-div{
    		visibility: hidden;
    	}
    	.hide-class{
    		display: none;
    	}
    </style>
</head>
<body>
	<ul id="discussList" class="mui-table-view">
		<!-- <li class="mui-table-view-cell mui-media">
			<a href="javascript:;">
				<img class="mui-media-object mui-pull-left" src="../../image/icon_taolun1.png" >
				<div class="mui-media-body">
					<div class="right-div">
						<div class="discuss-del-div">
							<img src="../../icon/js_delete2.png" class="discuss-del">
						</div>
						<p class="mui-ellipsis font12 base_grey content_p right-tag">
							未参与
						</p>
					</div>
					<div class="left-div">
						<p class="mui-ellipsis base_dark_grey font14 cell-title">
							<span class="message_title">
								讨论名称
							</span>  
						</p>
						<p class="mui-ellipsis base_grey font12 content_p cell-subtitle">
							物流仓储一班
						</p>
						<p class="mui-ellipsis base_grey font12 content_p cell-subtitle">
							<span class="file_submit_time">
								2017-01-02 14:20
							</span>
						</p>
					</div>
					<div class="tap-down-btn">
						<img src="../../image/arrow_down.png" class="tap-down-btn-img">
					</div>
				</div>
			</a>
		</li> -->
		<!-- <li class="mui-table-view-cell mui-media">
			<a href="javascript:;">
				<img class="mui-media-object mui-pull-left" src="../../image/icon_taolun1.png" >
				<div class="mui-media-body">
					<div class="right-div">
						<p class="mui-ellipsis font12 base_grey content_p right-tag">
							0/14人参加
						</p>
						<p class="mui-ellipsis font12 base_grey content_p right-tag">
							未参与
						</p>
					</div>
					<div class="left-div">
						<p class="mui-ellipsis base_dark_grey font14 cell-title">
							<span class="message_title">
								讨论名称
							</span>  
						</p>
						<p class="mui-ellipsis base_grey font12 content_p cell-subtitle">
							<span class="file_submit_time">
								2017-01-02 14:20
							</span>
						</p>
					</div>
					<div class="tap-down-btn">
						<img src="../../image/arrow_down.png" class="tap-down-btn-img">
					</div>
				</div>
			</a>
		</li> -->
	</ul>
	<div class="noSign" hidden="hidden" id="noData">
		<img src="../../image/icon_img.png" width="60%">
		<p class="font16 top_15 base_dark_grey" >
			暂无数据
		</p>
	</div>
	<div class="loadFailure" hidden="hidden" id="noNet">
		<img src="../../image/icon_img01.png" width="60%">
		<p class="" >
			数据加载失败
		</p>
		<p>
			请检查您的网络
		</p>
		<button type="button" class="mui-btn mui-btn-outlined" onclick="refreshData()">
			重新加载
		</button>
	</div>
	<div id="removeDiscussAlert" class="weui_dialog_confirm" style="display: none;">
        <div class="weui_mask"></div>
        <div class="weui_dialog">
            <div class="weui_dialog_hd">
                <strong class="weui_dialog_title">是否确定删除该讨论？</strong>
            </div>
            <div class="weui_dialog_bd"></div>
            <div class="weui_dialog_ft">
                <a onclick="cleanCancel()" href="#" class="weui_btn_dialog default">取消</a>
                <a onclick="removeDiscuss()" href="#" class="weui_btn_dialog primary"  style="color:#18b4ed">确定</a>
            </div>
        </div>
    </div>
</body>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/jquery.min.js"></script>
<script type="text/javascript" src="../../script/MD5.js"></script>
<script type="text/javascript" src="../../script/requestTool.js"></script>
<script type="text/javascript" src="../../script/timeStampTool.js"></script>
<script type="text/javascript" src="../../script/activityBottomPopup.js"></script>
<script type="text/javascript">
	var page = 1;
	var rows = 10;
	var dropRefreshCount = 0;
	var discussDatas = new Array();
	var firstLoad = true;
	var deleteDiscussData = null;
	apiready = function(){
		setupNotifi();
	    loadNewData();
		setRefreshHeader();
		setPullUpRefresh();
		sendBuriedNotifi('0', '老师端讨论列表', '205')
	}
	//添加新的讨论后，返回刷新列表
	function setupNotifi(){
		api.addEventListener({
		    name: 'refreshDiscussListNotifi'
		}, function(ret, err) {
		    refreshData();
		});
		api.addEventListener({
		    name: 'discussUpdateStatusNotifi'
		}, function(ret, err) {
		    var id = ret.value.id;
		    var index = $('#'+id).attr('index');
		    if (index != null) {
		    	var disData = discussDatas[parseInt(index)];
		    	disData["status"] = 1;
		    	var disStatusStr = '0/' + disData.countnum + '人回复';
				$('#status'+id).text(disStatusStr);
		    }
		});
		api.addEventListener({
		    name: 'pushMessage_classroomMessage'
		}, function(ret, err) {
		    var notifiJid = ret.value.jid;
		    //判断是否同一集团
		    if (notifiJid == $api.getStorage('cfnetppjxid')) {
		    	var type = ret.value.type;
			    if (type == 10011) {
			    	loadNewData();
			    }
		    }
		});
	}
	function sendBuriedNotifi(busionessid, pagetype, pagetypeid) {
		var userinfo = {
			uid : $api.getStorage('cfnetppuid'),
			jid : $api.getStorage('cfnetppjxid'),
			uname : $api.getStorage('cfnetppusername'),
			cid : api.pageParam.cid,
		    pid : api.pageParam.pid
		}
		var data = {
			busionessid : busionessid,
			pagetype : pagetype,
			pagetypeid : pagetypeid,
			sourceurl : api.pageParam.sourceurl,
			sourcetitle : api.pageParam.sourcetitle,
			accesspageurl : window.location.pathname,
			accesspagetitle : document.title,
			userinfo : userinfo
		}
		SendBehaviorTrajectoriesNotifi_Study(data);
	}
	function loadNewData() {
		page = 1;
		loadData();
	}
	function loadMoreData() {
		page++;
		loadData();
	}
	//下拉刷新
	function setRefreshHeader() {
		api.setRefreshHeaderInfo({
			visible : true,
			bgColor : '#ccc',
			textColor : '#fff',
			textDown : '下拉刷新...',
			textUp : '松开刷新...',
			showTime : true
		}, function(ret, err) {
			loadNewData();
		});
	}
	// 上拉加载
	function setPullUpRefresh() {
		api.addEventListener({
			name : 'scrolltobottom'
		}, function(ret, err) {
			if (dropRefreshCount >= rows) {
				dropRefreshCount = 0;
				loadMoreData();
			}
		});
	}
	function loadData(){
		if (firstLoad == true) {
			api.showProgress({
			    style: 'default',
			    animationType: 'fade',
			    title: '加载中...',
			    text: '',
			    modal: false
			});
			firstLoad = false;
		}
		var param = {
			uid : $api.getStorage('cfnetppuid'),
			jid : $api.getStorage('cfnetppjxid'),
			page : page,
			pagesize : rows,
			tid : 4,
			cid : api.pageParam.cid,
			pid : api.pageParam.pid
		};
		// var paste = api.require('pasteboard');
		// paste.paste({ value: JSON.stringify(param)+'\n'+url_DiscussList }, function(ret, err) {
		//     if (ret.status) {
		//         api.alert({
		//             title: '返回信息',
		//             msg: 'statues:' + ret.status,
		//             buttons: ['确定']
		//         }, function(ret, err) {});
		//     } else {
		//         api.alert({
		//             title: '返回信息',
		//             msg: 'msg:' + ret.msg + 'code:'+err.code,
		//             buttons: ['确定']
		//         }, function(ret, err) {});
		//     }
		// });
		cfnetppPOST(url_activeList, param, true, function(data, status){
			api.refreshHeaderLoadDone();
			api.hideProgress();
			if (status == 'success') {
				if (data.code == 200) {
					var disArr = data.data;
					if (disArr != null && disArr.constructor == Array && disArr.length > 0) {
						var currentIndex = discussDatas.length;
						dropRefreshCount = disArr.length;
						discussDatas = discussDatas.concat(disArr);
						var disHtmlString = new String();
						for (var i = currentIndex; i < discussDatas.length; i++) {
							var disData = discussDatas[i];
							disHtmlString += discussHtmlWithData(disData, i);
						}
						if (page == 1) {
							$('#discussList').html(disHtmlString);
						}else{
							$('#discussList').append(disHtmlString);
						}
						addCellClick();
						$('#noData').hide();
						$('#noNet').hide();
					}else{
						if (page == 1) {
							$('#discussList').html('');
							$('#noData').show();
							$('#noNet').hide();
						}
					}
				}else{
					if (page == 1) {
						$('#discussList').html('');
						$('#noData').show();
						$('#noNet').hide();
					}
				}
			}else{
				if (page > 1) {
					page--;
					dropRefreshCount = rows;
					api.toast({
						msg : '网络异常'
					});
				}else{
					$('#discussList').html('');
					$('#noData').hide();
					$('#noNet').show();
				}
			}
		});
	}
	function discussHtmlWithData(data, index){
		var disStatusStr = '未开始';
		var disNum = data.num;
		var allNum = data.countnum;
		var tapClass = '';
		var deleteBtn = '<div class="discuss-del-div" index="'+index+'"><img src="../../icon/js_delete2.png" class="discuss-del"></div>';
		var peopleNum = disNum + '/' + allNum + '人回复';
		var peopleNumClass = 'hide-v-div';//默认隐藏，但是仍然占位
		if (data.status == 0) {//未开始
			disStatusStr = "未开始";
		}else if(data.status == 1){//已开始
			disStatusStr = '进行中';
			deleteBtn = '<div class="discuss-none-del-div"><img src="../../icon/js_delete2.png" class="discuss-none-del"></div>';
		}else if (data.status == 2) {//已结束
			disStatusStr = "已结束";
			tapClass = 'hide-class';
			peopleNumClass = '';
		}
		// return '<li id="'+data.id+'" class="mui-table-view-cell mui-media discuss-cell" index="'+index+'">'+
		// 	'<a href="javascript:;">'+
		// 		'<img class="mui-media-object mui-pull-left" src="../../image/icon_taolun1.png" >'+
		// 		'<div class="mui-media-body">'+
		// 			'<div class="right-div">'+
		// 				deleteBtn+
		// 				'<p class="mui-ellipsis base_grey content_p font12 right-tag">'+
		// 					disStatusStr+
		// 				'</p>'+
		// 			'</div>'+
		// 			'<div class="left-div">'+
		// 				'<p class="mui-ellipsis base_black_grey font14 cell-title">'+
		// 					'<span class="message_title">'+
		// 						data.name+
		// 					'</span>'+
		// 				'</p>'+
		// 				'<p class="mui-ellipsis base_grey font12 content_p cell-subtitle">'+
		// 					data.classname+
		// 				'</p>'+
		// 				'<p class="mui-ellipsis base_grey font12 content_p cell-subtitle">'+
		// 					'<span class="file_submit_time">'+
		// 						timeWithStamp3(data.createtime)+
		// 					'</span>'+
		// 				'</p>'+
		// 			'</div>'+
		// 			'<div class="tap-down-btn">'+
		// 				'<img src="../../image/arrow_down.png" class="tap-down-btn-img">'+
		// 			'</div>'+
		// 		'</div>'+
		// 	'</a>'+
		// '</li>';
		return '<li id="'+data.id+'" class="mui-table-view-cell mui-media discuss-cell" index="'+index+'">'+
			'<a href="javascript:;">'+
				'<img class="mui-media-object mui-pull-left" src="../../image/icon_taolun1.png" >'+
				'<div class="mui-media-body">'+
					'<div class="right-div">'+
						'<p id="peopleNum'+data.id+'" class="mui-ellipsis base_grey content_p font12 right-tag '+peopleNumClass+'">'+
							peopleNum+
						'</p>'+
						'<p id="status'+data.id+'" class="mui-ellipsis base_grey content_p font12 right-tag">'+
							disStatusStr+
						'</p>'+
					'</div>'+
					'<div class="left-div">'+
						'<p class="mui-ellipsis base_black_grey font14 cell-title">'+
							'<span class="message_title">'+
								data.name+
							'</span>'+
						'</p>'+
						'<p class="mui-ellipsis base_grey font12 content_p cell-subtitle">'+
							'<span class="file_submit_time">'+
								timeWithStamp3(data.createtime)+
							'</span>'+
						'</p>'+
					'</div>'+
					'<div id="tap'+data.id+'" index="'+index+'" class="tap-down-btn '+tapClass+'">'+
						'<img src="../../image/icon_bewrite_down.png" class="tap-down-btn-img">'+
					'</div>'+
				'</div>'+
			'</a>'+
		'</li>';
	}
	function cleanCancel(){
		$('#removeDiscussAlert').hide();
	}
	function removeDiscuss(){
		$('#removeDiscussAlert').hide();
		var param = {
			aid : deleteDiscussData.id,
			uid : $api.getStorage('cfnetppuid'),
			jid : $api.getStorage('cfnetppjxid')
		};
		cfnetppPOST(url_DiscussDel, param, false, function(data, status){
		   	SendBusinessOperationsLogNotifi('老师端讨论删除', '205', param, param, '', data, '老师端讨论删除接口  ', '205-02')
			if (status == 'success') {
				if(data.code == 200){
					$('#'+deleteDiscussData.id).remove();
					api.toast({
    				    msg: '删除成功'
    				});
    				if($('#discussList').html() == ''){//如果删除最后一条则显示无内容
						$('#noData').show();
						$('#noNet').hide();
    				}
				}else{
					api.toast({
    				    msg: '删除失败，请重试！'
    				});
				}
			}else{
				api.toast({
				    msg: '网络异常删除失败'
				});
			}
		});
	}
    function addCellClick(){
    	$('.tap-down-btn').unbind('click');
    	$('.tap-down-btn').click(function(event){
    		var tapImg = $(this).children('.tap-down-btn-img');
    		tapImg.attr('src', '../../image/icon_bewrite_up.png');//向上
    		var index = parseInt($(this).attr('index'));
    		var disData = discussDatas[index];
    		var popupData = {
    			topTitle : disData.name
    		};
    		if (disData.status == 1) {
    			popupData['pTag1'] = {
					imgUrl : '../../image/icon_activity_del_disabled.png',
					title : '删除',
					textcolor : '#999999'
				}
				popupData['pTag2'] = {
					imgUrl : '../../image/icon_activity_edit_disabled.png',
					title : '编辑',
					textcolor : '#999999'
				}
    			popupData['pTag3'] = {
					imgUrl : '../../image/icon_activity_end.png',
					title : '结束'
				}
    		}else if(disData.status == 2){
    			popupData['pTag1'] = {
					imgUrl : '../../image/icon_activity_del_disabled.png',
					title : '删除',
					textcolor : '#999999'
				}
				popupData['pTag2'] = {
					imgUrl : '../../image/icon_activity_edit_disabled.png',
					title : '编辑',
					textcolor : '#999999'
				}
    			popupData['pTag3'] = {
					imgUrl : '../../image/icon_activity_end_disabled.png',
					title : '已结束',
					textcolor : '#999999'
				}
    		}
    		var popup = new activityBottomPopupObjc();
    		popup.openBottomPopup(popupData, function(tag){
    			if(tag == 1){//删除
    				if (disData.status != 0) {
    					return;
    				}
    				deleteDiscussData = disData;
    				$('#removeDiscussAlert').show();
    			}else if(tag == 2){//编辑
    				if (disData.status == 0) {
			    		disData["usertype"] = 'teacher';
			    		disData["activestatus"] = disData.status;
			    		disData["cid"] = api.pageParam.cid;
			    		disData["pid"] = api.pageParam.pid;
			    		disData["sourceurl"] = api.pageParam.sourceurl;
			    		disData["sourcetitle"] = api.pageParam.sourcetitle;
			    		disData["editType"] = 'edit';
			    		api.openWin({useWKWebView:true,
			    		    name: 'Discuss_TeacherEdit',
			    		    url: 'Discuss_TeacherEdit.html',
			    		    pageParam: disData,
			    		    slidBackEnabled: false
			    		});
    				}else{
    					// api.toast({
    					//     msg: '活动已开始不能编辑'
    					// });
    					return;
    				}
    			}else if(tag == 3){
    				if (disData.status == 2) {
    					// api.toast({
    					//     msg: '活动已结束'
    					// });
    					return;
    				}
    				startOrEndActity(disData);
    			}
    			popup.closeBottomPopup();
    			tapImg.attr('src', '../../image/icon_bewrite_down.png');//向下
    		});
    		event.stopPropagation();
    	});
    	$('.discuss-del-div').unbind('click');
    	$('.discuss-del-div').click(function(event){
    		var index = parseInt($(this).attr('index'));
    		var disData = discussDatas[index];
    		deleteDiscussData = disData;
    		$('#removeDiscussAlert').show();
    		event.stopPropagation();
    	});
    	$('.discuss-cell').unbind('click');
    	$('.discuss-cell').click(function(){
    		var index = parseInt($(this).attr('index'));
    		var disData = discussDatas[index];
    		var winName = 'Discuss_TeacherDetailWin';
    		var winPath = 'Discuss_TeacherDetailWin.html';
    		disData["usertype"] = 'teacher';
    		disData["activestatus"] = disData.status;
    		disData["cid"] = api.pageParam.cid;
    		disData["pid"] = api.pageParam.pid;
    		disData["sourceurl"] = api.pageParam.sourceurl;
			 disData["sourcetitle"] = api.pageParam.sourcetitle;
    		if (disData.status == 0) {//未开始则可编辑修改
    			winName = 'Discuss_TeacherEdit';
				winPath = 'Discuss_TeacherEdit.html'; 
				disData["editType"] = 'edit';
    		}
    		api.openWin({useWKWebView:true,
    		    name: winName,
    		    url: winPath,
    		    pageParam: disData,
    		    slidBackEnabled: false
    		});
    	});
    }
    function startOrEndActity(disData){
    	var action;
    	var resultStatus;
    	if(disData.status == 0){
    		action = 'start';
    		resultStatus = 1;
    		api.showProgress({
    		    style: 'default',
    		    animationType: 'fade',
    		    title: '',
    		    text: '讨论开启中...',
    		    modal: false
    		});
		}else{//结束
			action = 'end';
			resultStatus = 2;
			api.showProgress({
    		    style: 'default',
    		    animationType: 'fade',
    		    title: '',
    		    text: '讨论关闭中...',
    		    modal: false
    		});
		}
    	var param = {
    		iid : disData.id,
    		tid : 4,
    		action : action 
    	};
    	cfnetppPOST(url_actionDosign, param, false, function(data, status){
    	   if (action == 'start') {
				SendBusinessOperationsLogNotifi('老师端讨论开启', '205', param, param, '', data, '老师端讨论开启接口 ', '205-01')
		   } else {
				SendBusinessOperationsLogNotifi('老师端讨论结束', '205', param, param, '', data, '老师端讨论结束接口  ', '205-03')
		   }
    		api.hideProgress();
    		if (status == 'success') {
    			if (data.code == 200) {
    				api.toast({
    				    msg: '操作成功'
    				});
    				setupDisStatus(disData.id, resultStatus);
    			}else{
    				api.toast({
    				    msg: '操作失败请重试！'
    				});
    			}
    		}else{
    			api.toast({
    			    msg: '网络异常请重试！'
    			});
    		}
    	});
    }
    function setupDisStatus(disId, status){
    	var id = disId;
	    var index = $('#'+id).attr('index');
	    if (index != null) {
	    	var disData = discussDatas[parseInt(index)];
	    	disData["status"] = status; 
	    	var statusStr;
	    	if (status == 1) {//开始
	    		// statusStr = '0/' + disData.countnum + '人回复';
	    		statusStr = '进行中';
	    	}else if(status == 2){//结束
	    		statusStr = '已结束';
	    		$('#tap'+id).addClass('hide-class');
	    		$('#peopleNum'+id).removeClass('hide-v-div');
	    	}
			$('#status'+id).text(statusStr);
	    }
    }
	function refreshData(){
		firstLoad = true;
		loadNewData();
	}
	function numNullStr(str){
		if (str == null || str == '' || str == undefined) {
			return 0;
		}
		return str;
	}  
</script>
</html>